<?php
namespace Home\Server;
use Home\Server\getRedisService;

/**
 * Class QueuePushService
 * @package Home\Server
 *
 * 包裹入库
 *      运单号 不存在     -> 插入包裹表        -> 插入微信、短信队列表
 *                  是否是合作快递公司 && 站点是否注册快递       -> 插入快递推送队列表
 *
 *      运单号 存在      -> 更新包裹表        -> 更新微信、短信队列表 (状态改为待发送)
 *
 * Author: 黄盖
 * Date: 2017-2-6 14:57
 * Info:
 */
class QueuePushService
{
    public $NowTime;                      //当前时间
    public $redis;                        //redis

    function __construct()
    {
        $this ->NowTime                 = time();

        //加载redis
        $this ->redis = ( new getRedisService() ) ->redis;
    }

    /**
     * Function getUserInfo
     * @param $stationID
     * @return mixed
     *
     * Author: 黄盖
     * Date: 2017-2-6 15:39
     * Info: 获取用户(站点)信息
     */
    public function getUserInfo( $stationID )
    {
        $stationID = intval( $stationID );
        $RedisKeyName = 'UserID_' . $stationID;

        if( $this ->redis )
        {

            //用户信息 用 0 数据库
            $this ->redis ->select( 0 );

            //获取用户(站点)信息
            $UserInfo = $this ->redis ->HgetAll( $RedisKeyName );

            if( ! empty( $UserInfo ) )
            {
                return $UserInfo;
            }
        }

        return ( new \Think\Model() ) ->table('tp_user')
            ->field('
                    `tp_user`.`f_user_id`,
                    `tp_user`.`f_address`, 
                    `tp_user`.`f_store_time`, 
                    `tp_user`.`f_user_mobile`, 
                    `tp_user`.`f_user_phone`, 
                    `tp_user`.`f_station_name`,
                    `tp_third_partner`.`f_partner_id`,
                    `tp_third_partner`.`f_partner_name`,
                    `tp_third_partner`.`f_partner_code`
                ')
            ->join('LEFT JOIN `tp_third_partner` ON `tp_user`.`f_user_id` = `tp_third_partner`.`f_station_id`')
            ->where(" `tp_user`.`f_user_id` = '{$stationID}' AND `tp_user`.`f_user_status` = 1")
            ->limit('1')
            ->find()
            ;
    }

    /**
     * Function getStationExpress_Info
     * @param $stationID
     * @param $expressID
     * @return mixed
     *
     * Author: 黄盖
     * Date: 2017-2-6 18:00
     * Info: 获取站点注册快递信息
     */
    public function getStationExpress_Info( $stationID, $expressID )
    {
        //站点注册快递信息
        return ( new \Think\Model() ) ->table('tp_station_register')
            ->field('`f_id`, `f_station_id`, `f_com_id`, `f_orgCode`, `f_empCode`, `f_stationCode`, `f_appkey`, `f_password`')
            ->where("`f_station_id` = '{$stationID}' AND `f_com_id` = '{$expressID}' AND `f_status` = 1")
            ->find()
        ;
    }

    /**
     * Function IntoParcelNotice
     * @param array $ParcelData
     *
     * Author: 黄盖
     * Date: 2017-2-6 14:59
     * Info: 插入通知队列表
     */
    public function IntoParcelNotice( $ParcelData = Array() )
    {
        if( is_array( $ParcelData ) && count( $ParcelData ) > 0 )
        {
            //用户(站点)信息
            $UserInfo = $this ->getUserInfo( $ParcelData[0]['f_station_id'] );

            foreach( $ParcelData as $PKey => $PVal )
            {
                $Putdata[] = Array(
                    'f_parcel_id'           => $PVal['f_parcel_id'],
                    'f_station_id'          => $PVal['f_station_id'],
                    'f_recipient_mobile'    => $PVal['f_recipient_mobile'],
                    'f_recipient_name'      => $PVal['f_recipient_name'],
                    'f_mail_no'             => $PVal['f_mail_no'],
                    'f_num'                 => $PVal['f_num'],
                    'f_take_code'           => $PVal['f_take_code'],
                    'f_company_name'        => $PVal['f_company_name'],
//                    'table_name'            => $PVal['table_name'],

                    'f_weixin_notice'       => $PVal['f_weixin_notice'],
                    'f_weixin_num'          => $PVal['f_weixin_num'],
                    'f_sms_notice'          => $PVal['f_sms_notice'],
                    'f_sms_num'             => $PVal['f_sms_num'],

                    'f_address'             => $UserInfo['f_address'],
                    'f_qtime'               => $UserInfo['f_store_time'],
                    'f_tel'                 => $UserInfo['f_user_phone'],
                    'f_station_name'        => $UserInfo['f_station_name'],
                    'f_speed_status'        => $PVal['f_speed_status'],
                    'f_create_time'         => date('YmdHis'),
                    'f_create_time_stamp'   => time()
                );
            }

            //类型转为
            $Putdata = DataType_Uniform( $Putdata );

            //插入队列
//            ( new \Think\Model() ) ->table('tp_parcel_notice_result') ->addAll(  $Putdata );
            $Notice = new \Home\Server\ParcelNoticeResultServer();
            $Notice->addAllParcelQueue( $Putdata );

        }
        else
        {
            return False;
        }
    }

    /**
     * Function UpdateParcelNotice
     * @param array $ParcelData
     * @return bool
     *
     * Author: 黄盖
     * Date: 2017-2-6 16:08
     * Info: 更新通知队列表  重置主体信息，重置 微信、短信 通知状态 为 0
     */
    public function UpdateParcelNotice( $ParcelData = Array() )
    {
        if( is_array( $ParcelData ) && count( $ParcelData ) > 0 )
        {
            //用户(站点)信息
            $UserInfo = $this ->getUserInfo( $ParcelData[0]['f_station_id'] );

            foreach( $ParcelData as $PKey => $PVal )
            {
                $Putdata = Array(
//                    'f_parcel_id'           => $PVal['f_parcel_id'],
                    'f_station_id'          => $PVal['f_station_id'],
                    'f_recipient_mobile'    => $PVal['f_recipient_mobile'],
                    'f_recipient_name'      => $PVal['f_recipient_name'],
                    'f_mail_no'             => $PVal['f_mail_no'],
                    'f_take_code'           => $PVal['f_take_code'],
                    'f_company_name'        => $PVal['f_company_name'],
//                    'table_name'            => $PVal['table_name'],

                    'f_address'             => $UserInfo['f_address'],
                    'f_qtime'               => $UserInfo['f_store_time'],
                    'f_tel'                 => $UserInfo['f_user_phone'],
                    'f_station_name'        => $UserInfo['f_station_name'],
                    'f_create_time'         => date('YmdHis'),
                    'f_create_time_stamp'   => time(),

                    'f_weixin_notice'       => 0,
                    'f_sms_notice'          => 0,
                    'f_weixin_num'          => 0,
                    'f_sms_num'             => 0,

                    'f_sms_content'             => '',
                    'f_sms_error'               => '',
                    'f_weixin_send_time'        => '',
                    'f_weixin_send_time_stamp'  => 0,
                    'f_sms_send_time'           => '',
                    'f_sms_send_time_stamp'     => 0
                );

                //类型转为
                $Putdata = DataType_Uniform( $Putdata );

                //更新队列
//                ( new \Think\Model() ) ->table('tp_parcel_notice_result') ->where("`f_parcel_id` = {$PVal['f_parcel_id']}") ->save( $Putdata );

                $Notice = new \Home\Server\ParcelNoticeResultServer();
                $Notice->updateParcelNotice( $PVal['f_parcel_id'] , $Putdata );

            }
        }
        else
        {
            return False;
        }
    }

    /**
     * Function IntoParcelExpress
     * @param array $ParcelData
     *
     * Author: 黄盖
     * Date: 2017-2-6 16:13
     * Info: 是否是合作快递公司 && 站点是否注册快递       -> 插入快递推送队列表
     */
    public function IntoParcelExpress( $ParcelData = Array() )
    {
        if( is_array( $ParcelData ) && count( $ParcelData ) > 0 )
        {
            //用户(站点)信息
            $UserInfo = $this ->getUserInfo( $ParcelData[0]['f_station_id'] );

            //站点注册快递信息
            if( ! isset( $UserInfo[ 'express_' . $ParcelData[0]['f_company_id'] ] ) )
            {
                $ExpressRegister = $this ->getStationExpress_Info( $ParcelData[0]['f_station_id'], $ParcelData[0]['f_company_id'] );

                $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] ] = $ExpressRegister['f_id'];

                //韵达快递其他参数
                if( $ExpressRegister['f_com_id'] == '109' )
                {
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_orgCode' ]       = $ExpressRegister['f_orgCode'];
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_stationCode' ]   = $ExpressRegister['f_stationCode'];
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_appkey' ]        = $ExpressRegister['f_appkey'];
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_password' ]      = $ExpressRegister['f_password'];
                }

                //圆通快递其他参数
                if( $ExpressRegister['f_com_id'] == '108' )
                {
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_orgCode' ]       = $ExpressRegister['f_orgCode'];
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_empCode' ]       = $ExpressRegister['f_empCode'];
                    $UserInfo[ 'express_' . $ExpressRegister['f_com_id'] . '_stationCode' ]   = $ExpressRegister['f_stationCode'];
                }

            }

            //如果快递ID 为 115 或者 182 直接插, 如果 为 109、125 并且已经注册, 则插入
            if( $ParcelData[0]['f_company_id'] == 115 || $ParcelData[0]['f_company_id'] == 182 || ( ( $ParcelData[0]['f_company_id'] == 109 || $ParcelData[0]['f_company_id'] == 125 || $ParcelData[0]['f_company_id'] == 108 ) && isset( $UserInfo[ 'express_' . $ParcelData[0]['f_company_id'] ] ) ) )
            {
                foreach( $ParcelData as $PKey => $PVal )
                {
                    $Putdata[] = Array(
                        'f_parcel_id'       => $PVal['f_parcel_id'],
                        'f_mail_no'         => $PVal['f_mail_no'],
                        'f_station_id'      => $PVal['f_station_id'],
                        'f_com_id'          => $PVal['f_company_id'],
                        'f_com_name'        => $PVal['f_company_name'],
                        'f_recipient_name'  => $PVal['f_recipient_name'],

                        'f_station_name'    => $UserInfo['f_station_name'],
                        'f_tel'             => $UserInfo['f_user_mobile'],
                        'f_create_time'     => date('YmdHis'),
                        'f_type'            => 1
                    );
                }

                //类型转为
                $Putdata = DataType_Uniform( $Putdata );

                //插入队列
                ( new \Think\Model() ) ->table('tp_station_abutment_log') ->addAll(  $Putdata );
            }
            
            //记录到第三方站点对接表
            if (!empty($UserInfo['f_partner_id'])) {
                $this->addStationPartnerLog($UserInfo, $ParcelData);
            }
        }
        else
        {
            return False;
        }
    }
    
    /**
     * 记录数据到第三方站点对接表
     * @author cheng
     */
    public function addStationPartnerLog($UserInfo, $ParcelData) {
        foreach ($ParcelData as $ParKey => $ParVal) {
            $PartnerData[] = Array(
                'f_parcel_id' => $ParVal['f_parcel_id'],
                'f_mail_no' => $ParVal['f_mail_no'],
                'f_take_code' => $ParVal['f_take_code'],
                'f_num' =>  $ParVal['f_num'],
                'f_station_id' => $ParVal['f_station_id'],
                'f_com_id' => $ParVal['f_company_id'],
                'f_com_name' => $ParVal['f_company_name'],
                'f_recipient_name' => $ParVal['f_recipient_name'],
                'f_recipient_mobile' => $ParVal['f_recipient_mobile'],
                'f_station_name' => $UserInfo['f_station_name'],
                'f_tel' => $UserInfo['f_user_mobile'],
                'f_create_time' => date('YmdHis'),
                'f_type' => 1
            );
        }

        //类型转为
        $PartnerData = DataType_Uniform($PartnerData);

        //插入队列
        ( new \Think\Model())->table('tp_station_partner_log')->addAll($PartnerData);
    }
}